/*
 * @Date: 2023-02-15 14:36:19
 * @LastEditTime: 2023-05-08 14:25:03
 * @FilePath: /liveness/src/utils/tools/Request.ts
 * 介绍:
 */
import axios, {
  AxiosInstance,
  AxiosRequestConfig,
  CreateAxiosDefaults,
} from "axios";
import { filterParams } from "@@/utils/tools/object";

export default class Request {
  /**axios实例 */
  axios: AxiosInstance;
  constructor(config?: CreateAxiosDefaults) {
    this.axios = axios.create(config);
    /**
     * * 请求拦截器
     */
    this.axios.interceptors.request.use(<any>((
      config: RequestConfig
    ): AxiosRequestConfig => {
      if (config.mode?.includes("removeReqVoid")) {
        config = filterParams(config);
      }
      // if (config.mode?.includes("showLoading")) {
      //   toast.loading();
      // }
      return config;
    }));
    /**
     * * 响应拦截器
     */
    this.axios.interceptors.response.use(
      (response) => {
        const data = response.data;
        return Promise.resolve(data);
      },
      (error) => {
        //对错误进行统一处理
        return Promise.resolve(error);
      }
    );
  }
  async get<T>(
    url: string,
    params: object | void,
    config?: RequestConfig
  ): Promise<T> {
    const res: any = await this.axios({
      ...config,
      url,
      params,
      method: "GET",
    });
    // if (config?.mode?.includes("showLoading")) toast.close();
    if (res.code === 200) {
      return this.successMessage(res, config?.mode);
    }
    return this.failMessage(res, config?.mode);
  }
  async post<T>(
    url: string,
    data: object | void,
    config?: RequestConfig
  ): Promise<T> {
    const res: any = await this.axios({
      ...config,
      url,
      data,
      method: "POST",
    });

    // if (config?.mode?.includes("showLoading")) toast.close();
    if (res.code === 200) {
      return this.successMessage(res, config?.mode);
    }
    return this.failMessage(res, config?.mode);
  }
  async put<T>(
    url: string,
    data: object | void,
    config?: RequestConfig
  ): Promise<T> {
    const res: any = await this.axios({
      ...config,
      url,
      data,
      method: "PUT",
    });
    // if (config?.mode?.includes("showLoading")) toast.close();
    if (res.code === 200) {
      return this.successMessage(res, config?.mode);
    }
    return this.failMessage(res, config?.mode);
  }
  async delete<T>(
    url: string,
    data: object | void,
    config?: RequestConfig
  ): Promise<T> {
    const res: any = await this.axios({
      ...config,
      url,
      data,
      method: "DELETE",
    });

    // if (config?.mode?.includes("showLoading")) toast.close();
    if (res.code === 200) {
      return this.successMessage(res, config?.mode);
    }
    return this.failMessage(res, config?.mode);
  }
  successMessage(res: any, mode: Mode[] | void): typeof res {
    if (!mode) return res;
    //@ts-ignore
    const { t } = i18n.global;
    if (mode.includes("showSucceed")) {
      // toast.success(res.msg || t("cheng-gong"));
    }
    if (mode.includes("removeResVoid")) {
      return (res.data = filterParams(res.data));
    }
    return res;
  }
  failMessage(res: any, mode: Mode[] | void): typeof res {
    if (!mode) return res;
    if (mode.includes("showError")) {
      // toast.error(res.msg, {
      //   duration: -1,
      //   closeOnClick: true,
      // });
    }
    return res;
  }
}
/**请求配置 */
export interface RequestConfig extends AxiosRequestConfig {
  mode?: Array<Mode>;
}
export type Mode =
  | "showError"
  | "showSucceed"
  | "showLoading"
  | "removeReqVoid"
  | "removeResVoid";
